package com.csii.modules.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.core.annotation.AutoLog;
import com.csii.core.api.Result;
import com.csii.core.system.base.controller.BaseController;
import com.csii.core.system.query.QueryGenerator;
import com.csii.core.util.ConvertUtils;
import com.csii.modules.system.controller.vo.OauthClientDetailsVO;
import com.csii.modules.system.entity.OauthClientDetails;
import com.csii.modules.system.service.IOauthClientDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: oauth_client_details
 * @Author: power-stdio
 * @Date:   2021-01-07
 * @Version: V1.0
 */
@Api(tags="oauth_client_details")
@RestController
@RequestMapping("/oauthClientDetails")
@Slf4j
public class OauthClientDetailsController extends BaseController<OauthClientDetails, IOauthClientDetailsService> {
	@Autowired
	private IOauthClientDetailsService oauthClientDetailsService;

	/**
	 * 分页列表查询
	 *
	 * @param oauthClientDetailsVO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "oauth_client_details-分页列表查询")
	@ApiOperation(value="oauth_client_details-分页列表查询", notes="oauth_client_details-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OauthClientDetailsVO oauthClientDetailsVO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		OauthClientDetails oauthClientDetails  = ConvertUtils.convert(OauthClientDetails.class,oauthClientDetailsVO);
		QueryWrapper<OauthClientDetails> queryWrapper = QueryGenerator.initQueryWrapper(oauthClientDetails, req.getParameterMap());
		Page<OauthClientDetails> page = new Page<OauthClientDetails>(pageNo, pageSize);
		IPage<OauthClientDetails> pageList = oauthClientDetailsService.page(page, queryWrapper);
		IPage<OauthClientDetailsVO> pageListVo = ConvertUtils.convertIPage(OauthClientDetailsVO.class, pageList);
		return Result.ok(pageListVo);
	}


	 /**
	  *查询ClientId列表
	  *
	  * @return
	  */
	 @AutoLog(value = "oauth_client_details-查询ClientId列表")
	 @ApiOperation(value="oauth_client_details-查询ClientId列表", notes="oauth_client_details-查询ClientId列表")
	 @GetMapping(value = "/queryClientIdList")
	 public Result<List<String>> queryClientIdList() {
		 List<OauthClientDetails> list = oauthClientDetailsService.list(new LambdaQueryWrapper<OauthClientDetails>().select(OauthClientDetails::getClientId));
		 if (CollUtil.isEmpty(list)){
		 	return Result.ok(new ArrayList<>());
		 }
		 List<String> clientIdList = list.stream().map(OauthClientDetails::getClientId).collect(Collectors.toList());
		 return Result.ok(clientIdList);
	 }

	/**
	 *   添加
	 *
	 * @param oauthClientDetailsVO
	 * @return
	 */
	@AutoLog(value = "oauth_client_details-添加")
	@ApiOperation(value="oauth_client_details-添加", notes="oauth_client_details-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody OauthClientDetailsVO oauthClientDetailsVO) {
		OauthClientDetails oauthClientDetails  = ConvertUtils.convert(OauthClientDetails.class,oauthClientDetailsVO);
		oauthClientDetailsService.save(oauthClientDetails);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param oauthClientDetailsVO
	 * @return
	 */
	@AutoLog(value = "oauth_client_details-编辑")
	@ApiOperation(value="oauth_client_details-编辑", notes="oauth_client_details-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody OauthClientDetailsVO oauthClientDetailsVO) {
		OauthClientDetails oauthClientDetails  = ConvertUtils.convert(OauthClientDetails.class,oauthClientDetailsVO);
		oauthClientDetailsService.updateById(oauthClientDetails);
		return Result.ok("编辑成功!");
	}

	/**
	 *   更新认证密钥
	 *
	 * @param clientId,clientSecret
	 * @return
	 */
	@ApiOperation(value="更新认证密钥", notes="更新认证密钥")
	@PostMapping(value = "/updateSecret")
	public Result<?> updateSecret(@RequestParam(name="clientId",required=true) String clientId,@RequestParam(name="clientSecret",required=true) String clientSecret) {

		oauthClientDetailsService.update(new LambdaUpdateWrapper<OauthClientDetails>().eq(OauthClientDetails::getClientId,clientId)
				.set(OauthClientDetails::getClientSecret,new BCryptPasswordEncoder().encode(clientSecret)));
		return Result.ok("删除成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "oauth_client_details-通过id删除")
	@ApiOperation(value="oauth_client_details-通过id删除", notes="oauth_client_details-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		oauthClientDetailsService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "oauth_client_details-批量删除")
	@ApiOperation(value="oauth_client_details-批量删除", notes="oauth_client_details-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.oauthClientDetailsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "oauth_client_details-通过id查询")
	@ApiOperation(value="oauth_client_details-通过id查询", notes="oauth_client_details-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		OauthClientDetails oauthClientDetails = oauthClientDetailsService.getById(id);
		if(oauthClientDetails==null) {
			return Result.error("未找到对应数据");
		}
		OauthClientDetailsVO oauthClientDetailsVO  = ConvertUtils.convert(OauthClientDetailsVO.class,oauthClientDetails);
		return Result.ok(oauthClientDetailsVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param oauthClientDetailsVO
    */
    @PostMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OauthClientDetailsVO oauthClientDetailsVO) {
        OauthClientDetails oauthClientDetails  = ConvertUtils.convert(OauthClientDetails.class,oauthClientDetailsVO);
        return super.exportXls(request, oauthClientDetails, OauthClientDetails.class, "oauth_client_details");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OauthClientDetails.class);
    }
}
